package com.qilin.hscs.controller;

import com.alibaba.fastjson.JSON;
import com.qilin.hscs.dto.UserDto;
import com.qilin.hscs.dto.UserModifyPassDto;
import com.qilin.hscs.form.UserInfoForm;
import com.qilin.hscs.pojo.User;
import com.qilin.hscs.enums.ResultEnum;
import com.qilin.hscs.exception.SMException;
import com.qilin.hscs.form.RegisterForm;
import com.qilin.hscs.form.ResetPassForm;
import com.qilin.hscs.service.SMSService;
import com.qilin.hscs.service.impl.UserServiceImpl;
import com.qilin.hscs.util.TokenUtil;
import com.qilin.hscs.vo.ResultVO;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private SMSService smsService;

    private static final Logger logger= LogManager.getLogger(UserController.class.getName());


    @PostMapping("/register")
    public ResultVO register(@RequestBody @Valid RegisterForm registerForm, BindingResult bindingResult)
    {
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        smsService.verify(registerForm.getPhone(), registerForm.getVerificationCode());
        User user =new User();
        BeanUtils.copyProperties(registerForm, user);
        return userService.register(user);
    }

    @PostMapping("/login")
    public ResultVO login(String username, String password) {
        logger.info("【用户登陆】={}",username);
        return userService.loginByPass(username,password);
    }

    @PostMapping(value = "/updatePassword")
    public ResultVO resetPassword(@RequestBody @Valid ResetPassForm resetPassForm,
                                  BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        smsService.verify(resetPassForm.getPhone(), resetPassForm.getVerificationCode());
        UserModifyPassDto dto = new UserModifyPassDto();
        BeanUtils.copyProperties(resetPassForm,dto);
        return userService.updatePassword(dto);
    }


    @PostMapping("/updateUserData")
    public ResultVO updateName(@Valid UserInfoForm form){
        UserDto dto=new UserDto();
        BeanUtils.copyProperties(form,dto);
        return userService.ModifyUserInfo(dto);
    }

    @GetMapping("/getUserData")
    public ResultVO findByPhone(String phone) {
        return userService.findUserByPhone(phone);
    }
}
